import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTitleActive: 0,
    scrollList: [],
    screeningLeftList: [],
    screeningRightList: [],
    toView: 'item0'
  },

  goVlogDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/square/details/index?id=${id}`,
    })
  },

  bindTitleActive(e) {
    console.log(e)
    const currentTitleActive = e.currentTarget.dataset.val;
    this.setData({
      currentTitleActive,
      toView: 'item'+currentTitleActive,
      screeningLeftList: [],
      screeningRightList: []
    })
    this.getComList()
  },

  getComList() {
    let params = {
      pageNo: this.data.pageNo || 1,
      pageSize: 10,
    }
    if (this.data.currentTitleActive != 0) {
      params.catId = this.data.currentTitleActive
    }
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/com/protected/vlog/findList',
      method: 'POST',
      data: params
    }, (res) => {
      console.log(res);
      if(res?.list) {
        let list = res.list.filter(item => item.auditStatus == 1)
        this.setData({
          pageNo: res.page.pageNo,
          hasNextPage: res.page.hasNextPage,
          screeningLeftList: this.data.screeningLeftList.concat(list.filter((item, index) => index % 2 == 0)),
          screeningRightList: this.data.screeningRightList.concat(list.filter((item, index) => index % 2 != 0))
        })
      }
    })
  },

  getCategory() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/com/protected/comm/beanCrud/com.CategoryVO/findAll',
      method: 'POST',
      data: {
        pageSize: 100
      }
    }, (res) => {
      console.log(res);
      let result = res.list.filter(item => item.level == 1)
      let all = {catName: '全部', id: "0" }
      result.unshift(all)
      console.log(result);
      this.setData({
        currentTitleActive: result[0].id,
        scrollList: result
      })
    })
    
  },

  openWrap () {
    this.setData({
      isOpen: true
    })
  },

  closeWrap () {
    this.setData({
      isOpen: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getComList();
    this.getCategory();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      screeningLeftList: [],
      screeningRightList: [],
      pageNo: 1
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //触底时，加载更多
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getComList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})